<?php 
include './functions.php';
$name=$_POST['name'];
$value=$_POST['value'];
switch ($name) {
	case 'username':
		checkUserName($name,$value);
		break;
	case 'email':
		checkEmail($name,$value);
		break;
	case 'password':
		checkPassWord($name,$value);
		break;
	// case 'passwordConfirm':
	// 	break;
}

function checkUserName($name,$value){
	$preg = "/^[A-z][A-z0-9]{3,7}$/";
	$c = preg_match($preg, $value);
	if(!$c){
		echo json_encode(['error'=>1,'info'=>"用户名需要是4-8位"]);exit;
	}
	// 读老数据,并转换json
	$data=DataJson::readJson('./Db/user.json');
	if(deep_in_array($value,$data)){//递归地查找多维数组的值是否存在
		echo json_encode(['error'=>1,'info'=>"用户名已存在"]);exit;
	}
	echo json_encode(['error'=>0,'info'=>"用户名可用"]);
}
function checkEmail($name,$value){
	$preg = "/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,})$/";
	$c = preg_match($preg, $value);
	if(!$c){
		echo json_encode(['error'=>1,'info'=>"邮箱格式不对"]);exit;
	}
	// 读老数据,并转换json
	$data=DataJson::readJson('./Db/user.json');
	if(deep_in_array($value,$data)){//递归地查找多维数组的值是否存在
		echo json_encode(['error'=>1,'info'=>"邮箱已存在"]);exit;
	}
	echo json_encode(['error'=>0,'info'=>"邮箱可用"]);
}
function checkPassWord($name,$value){
	$preg = "/^[A-z0-9]{3,10}$/";
	$c = preg_match($preg, $value);
	if(!$c){
		echo json_encode(['error'=>1,'info'=>"密码需要是4-10位"]);exit;
	}
	echo json_encode(['error'=>0,'info'=>"密码可用"]);
}


 ?>